# !/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2020/10/26 18:07
# @Author  : allan.xie
# @File    : hrunxls.py
# @Software: PyCharm
import logging
import optparse
import os
# from core.conf.config import settings
import sys

curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = os.path.split(curPath)[0]
sys.path.append(rootPath)

# sys.path.append(settings.BASE_DIR)  # 添加项目环境变量

from hruntools.file2xls import loadfile2excel
from hruntools.call_xls import call_excel


class Main(object):
    def __init__(self):
        usage = """python hrunxls.py -i <input file> -x <excel file>
                                    -r  <excel file> [-t [ALL|TestSuite|SheetSuite]]
                                    -e -l [DEBUG|INFO|WARNING]"""
        # 用于显示帮助信息

        self.parser = optparse.OptionParser(usage)
        self.parser.add_option("-r", "--runexcel", dest="runexcel", help="run hrun excel testcase")
        self.parser.add_option("-i", "--infile", dest="infile", help="input file(yml/json/har)")
        self.parser.add_option("-x", "--toexcel", dest="toexcel", help="file(yml/json/har) to excel file")
        self.parser.add_option("-e", "--extendreport", action="store_true", dest="extendreport",
                               help="Extend Report teamplate or default template ", default=False)
        self.parser.add_option("-l", "--loglevel", dest="loglevel", help="loglevel  DEBUG, INFO, WARNING",
                               default="DEBUG")
        self.parser.add_option("-p", "--reporturlpath", dest="reporturlpath", help="report url path ",
                               default="")

        self.parser.add_option("-t", "--type", dest="sheetype", help="sheets type: All|TestSuite|SheetSuite"
                               # , default="TestSuite"
                               )

        self.options, self.args = self.parser.parse_args()  # options是对象，args是List
        logging.basicConfig(level="DEBUG")

        if getattr(self.options, "runexcel"):  # 根据文件(yml/json/har)生成excel文件
            logging.info("Start to run excel file.")
            if self.options.runexcel:  # and "har" in self.options.testcase:
                excelfile = self.options.runexcel
                print(excelfile)
                if self.options.sheetype:
                    # 多sheet 测试，按照sheetype 控制
                    call_excel(excelfile, loglevel=self.options.loglevel, isExtendReport=self.options.extendreport,
                               reportURL=self.options.reporturlpath,
                               sheetType=self.options.sheetype)
                else:
                    # 默认执行单 sheet 测试用例
                    call_excel(excelfile, loglevel=self.options.loglevel, isExtendReport=self.options.extendreport,
                               reportURL=self.options.reporturlpath)


        if getattr(self.options, "infile"):  # 根据文件(yml/json/har)生成excel文件
            logging.info("Start to generate excel file.")
            infile = ''
            if self.options.infile:  # and "har" in self.options.testcase:
                infile = self.options.infile

            toexcel = self.options.toexcel if self.options.toexcel \
                else "%s.xls" % ".".join((os.path.basename(infile).split('.')[0:-1]))

            # todo 指定excel路径
            toexcel = os.path.join(os.path.dirname(infile),toexcel)

            print(infile +'  '+ toexcel)
            loadfile2excel(infile, toexcel)

        if len(self.args) > 0:
            print(self.args[0])
        # elif len(self.options)

        # print(self.options.loglevel)
        # print(str(self.options.extendreport))


if __name__ == '__main__':
    Main()
